package registnet.records.source;

import grammar.PrerequisiteCruncher;

import java.util.List;
import java.util.Vector;

import registnet.records.Curriculum;
import registnet.records.CurriculumCourse;
import registnet.records.Department;
import registnet.records.IPrerequisite;
import registnet.records.Prerequisite;

public class CurriculumCourseSource {

	private String courseCode;
	private Curriculum curriculum;
	private int semester;
	private int year;
	private CurriculumCourse curriculumCourse;
	
	
	/**
	 * Default constructor
	 * 
	 * @param courseCode - The code assigned to the course
	 * @param curriculum - The curriculum that makes use of the curriculum course
	 * @param semester - The semester when the curriculum course should be taken
	 * @param year - The year level when the curriculum course should be taken
	 */
	public CurriculumCourseSource(String courseCode, Curriculum curriculum, int semester, int year){
		this.courseCode = courseCode;
		this.curriculum = curriculum;
		this.semester = semester;
		this.year = year;
		this.load();
	}
	
	
	/**
	 * Loads data from source
	 */
	public void load(){
		try{
			registnet.dao.Curriculum DAOCurriculum = new registnet.dao.CurriculumPeer().getCurriculum(this.curriculum.getCurriculumCode());
			registnet.dao.Course DAOCourse = new registnet.dao.CoursePeer().getCourse(this.courseCode);
			registnet.dao.CurriculumCourse DAOCurriculumCourse = new registnet.dao.CurriculumCoursePeer().getCurriculumCourse(DAOCurriculum.getCurriculumId(), DAOCourse.getCourseId(), this.semester, this.year);
			Department department = new DepartmentSource(DAOCourse.getUnit().getUnitCode()).getDepartment();
			this.curriculumCourse = new CurriculumCourse(this.courseCode, this.curriculum, this.semester, this.year);
			this.curriculumCourse.setCourseName(DAOCourse.getCourseName());
			this.curriculumCourse.setDepartment(department);
			this.curriculumCourse.setUnit(DAOCourse.getUnits());
			this.curriculumCourse.setPrerequisites(this.getPrerequisites(DAOCurriculumCourse.getPrerequisite()));
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Gets the prerequisites of the course
	 * 
	 * @param source - The prerequisites of the course, in text
	 * @return	The prerequisites of the course, in list
	 */
	private List<IPrerequisite> getPrerequisites(String source){
		List<IPrerequisite> prerequisites = new Vector<IPrerequisite>();
		List<String> temp = PrerequisiteCruncher.getPrerequisites(source);
		for(int i=0; i<temp.size(); i++){
			Prerequisite prerequisite = new Prerequisite(temp.get(i));
			prerequisites.add(prerequisite);
		}
		return prerequisites;
	}
	
	
	/**
	 * Gets the curriculum course
	 * 
	 * @return	The curriculum course
	 */
	public CurriculumCourse getCurriculumCourse(){
		return this.curriculumCourse;
	}
}
